package com.silence.springboot.common;

import com.silence.springboot.enums.ResultCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author silence
 * @email 792245022@qq.com
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Schema(name = "Result", description = "共通返回结果")
public class Result<T> implements Serializable {

    /**
     * 序列化标识符
     */
    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 响应码
     */
    @Schema(description = "响应码")
    private int code;

    /**
     * 响应数据
     */
    @Schema(description = "响应数据")
    private T data;

    /**
     * 响应消息
     */
    @Schema(description = "响应消息")
    private String msg;

    /**
     * 成功
     *
     * @param data 响应数据
     * @param <T>  响应数据类型
     * @return Result
     */
    public static <T> Result<T> success(T data) {
        return Result.<T>builder()
                .code(ResultCode.SUCCESS.getCode())
                .data(data)
                .msg(ResultCode.SUCCESS.getMsg())
                .build();
    }

    /**
     * 成功
     *
     * @param <T>  响应数据类型
     * @return Result
     */
    public static <T> Result<T> success() {
        return Result.<T>builder()
                .code(ResultCode.SUCCESS.getCode())
                .msg(ResultCode.SUCCESS.getMsg())
                .build();
    }

    /**
     * 失败
     *
     * @param resultCode 响应码
     * @param <T>        响应数据类型
     * @return Result
     */
    public static <T> Result<T> failed(ResultCode resultCode) {
        return Result.<T>builder()
                .code(resultCode.getCode())
                .msg(resultCode.getMsg())
                .build();
    }

}
